// ssh -vnNt -R 3001:localhost:3000 root@test.huxinfeng.com
// 导入加密模块
const crypto = require("crypto");

const convert = require("xml-js");

const sign = (ctx, next) => {
	const { signature, echostr, timestamp, nonce } = ctx.request.query;

	// 微信公众号的 token
	const token = "huxinfeng";
	/*
	 * 验证请求是否来自微信公众平台
	 */
	const arr = [token, timestamp, nonce].sort();
	const str = arr.join("");
	// 加密
	const sign = crypto.createHash("sha1").update(str).digest("hex");

	if (sign === signature) {
		// 返回接口调用者
		ctx.body = echostr;
	} else {
		ctx.body = "error";
	}
	console.log("sign");
};

const reply = (ctx, next) => {
	// const body = ctx.request.body;
	// const js = convert.xml2js(body, {
	// 	compact: true,
	// });
  // console.log(js);
  console.log(1);
};

// 暴露接口
module.exports = {
	sign,
	reply,
};
